<?php 
	require_once("MoneyFactory.class.php"); 
	require_once("PorteMonnaie.class.php");


	if(isset($_POST['m2'])) {
		$m2 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m2'],"EUR");
		$m3 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m3'],"USD");
		$m4 = MoneyFactory::getDefaultFactory()->createMoney($_POST['m4'],"CHF");
		$wallet =[$m2,$m3,$m4];
	}
	else{
			$m2 = MoneyFactory::getDefaultFactory()->createMoney(0,"EUR");
			$m3 = MoneyFactory::getDefaultFactory()->createMoney(0,"USD");
			$m4 = MoneyFactory::getDefaultFactory()->createMoney(0,"CHF");

			echo "<div id='result'>Votre porte monnaie contient : <br/>";

			echo "(EUR) 0 <br/>(USD) 0 <br/>(CHF) 0 <br/>  </div>";

			$wallet =[$m2,$m3,$m4];
	}

	function processRequest($request) {
		global $m2;
		global $m3;
		global $m4;
		global $wallet;

		if (isset($request["ops"]) && isset($request["v1"]) && isset($request["c1"]))
		{
			$m1=MoneyFactory::getDefaultFactory()->createMoney($request["v1"],$request["c1"]);
			
			$opsCode=$request["ops"];

			echo "<div id='result'>Votre porte monnaie contient : </div>";
			echo "<div id='result_detail'>";

			try{
				if (strcmp($opsCode,"ADD")==0) {
					$res=PorteMonnaie::addMulti($m1,$wallet);

				}
				if (strcmp($opsCode,"REM")==0) {
					$res=PorteMonnaie::supprMulti($m1,$wallet);
				}

			} catch (NotEnoughMoneyException $e){
				$res=$wallet;
				echo "<script type=\"text/javascript\">
   				 alert(\"Vous n\'avez pas assez d\'argent pour effectuer cette opération\");
  				</script>";

			}
			
			if (isset($res)) {
				echo "(",$res[0]->getCurrency(),") ",$res[0]->getValue();
				echo "<br/>(",$res[1]->getCurrency(),") ",$res[1]->getValue();
				echo "<br/>(",$res[2]->getCurrency(),") ",$res[2]->getValue();

			} 
			else echo "Unsupported operations"; 
			

			$m2 = MoneyFactory::getDefaultFactory()->createMoney($res[0]->getValue(),$res[0]->getCurrency());
			$m3 = MoneyFactory::getDefaultFactory()->createMoney($res[1]->getValue(),$res[1]->getCurrency());;
			$m4 = MoneyFactory::getDefaultFactory()->createMoney($res[2]->getValue(),$res[2]->getCurrency());;



			echo "</div>";
			
		}
	
	}
?>
<html>
	<head>
		<title>Money Operations</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	</head>
	<body>
		<?php
		processRequest($_REQUEST);	
		?>
		<hr width='20%' align='left'/>
		<form action='index.php' method='post' id='form0'>
			<input type='hidden' id='m2' name='m2' value='<?php echo $m2->getValue();?>'/>
			<input type='hidden' id='m3' name='m3' value='<?php echo $m3->getValue(); ?>'/>
			<input type='hidden' id='m4' name='m4' value='<?php echo $m4->getValue(); ?>'/>
			
		<table>
			<tr>
			  <td>Valeur</td><td>Devise</td><td>Opération</td>
			</tr>
			<tr>
			  <td><input type='text' id='v1' name='v1' value='<?php echo $_REQUEST["v1"];?>'/></td>
			   <td><input type='text' id='c1' name='c1' value='<?php echo $_REQUEST["c1"];?>'/></td>
			  <td><select name='ops'><option value='ADD'>+</option><option value='REM'>-</option></select></td>
			</tr>
			<tr>
			   <td colspan='5' align='right'><input type='submit' value='calculer'/></td>			
			</tr>
		</table>
		</form>
	</body>
</html>
